package com.sicilin.object.util;

import com.sicilin.common.tree.TreeObject;
import com.sicilin.object.dto.ObjectDTO;
import com.sicilin.object.enums.ObjectTypeEnum;
import com.sicilin.po.TCObject;
import com.sicilin.object.exception.ObjectServiceException;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsDateJsonBeanProcessor;
import org.apache.commons.lang.StringUtils;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ObjectUtils {

    public static boolean validSaveObject(TCObject object) throws ObjectServiceException{
        if(object == null){throw new ObjectServiceException("Invalid TCObject parameter!");}
        if(StringUtils.isBlank(object.getObjectCode())){throw new ObjectServiceException("objectCode is blank!");}
        if(StringUtils.isBlank(object.getObjectName())){throw new ObjectServiceException("objectName is blank!");}
        if(StringUtils.isBlank(object.getPosition())){throw new ObjectServiceException("position is blank!");}
        if(StringUtils.isBlank(object.getTotalCode())){throw new ObjectServiceException("totalCode is blank!");}
        if(object.getType() == null || object.getType().equals(0)){throw new ObjectServiceException("type is null or 0!");}
        if(object.getGeneration() == null || object.getGeneration().equals(0)){throw new ObjectServiceException("generation is null or 0!");}
        if(object.getState() == null ){throw new ObjectServiceException("state is null!");}
        if(object.getParentId() == null){throw new ObjectServiceException("parentId is null!");}
        if(object.getHasChildren() == null){throw new ObjectServiceException("hasChildren is null!");}
        if(object.getCompanyId() == null){throw new ObjectServiceException("companyId is null!");}
        return true;
    }


    public static String nextPosition(String currentPosition) throws ObjectServiceException{
        if(StringUtils.isBlank(currentPosition) || currentPosition.equals("null")){
            return "000001";
        }
        if(!StringUtils.isNumeric(currentPosition)){
            throw new ObjectServiceException("Invalid currentPosition! It's not a number!");
        }
        DecimalFormat df = new DecimalFormat("######");
        int tmp = Integer.valueOf(currentPosition)+1;

        return String.format("%06d",tmp);
    }

    public static List<TreeObject<TCObject>> changeToTreeObject(Collection<TCObject> tcObjects) throws ObjectServiceException{
        if(tcObjects == null){throw new ObjectServiceException("Invalic tcObjects param! It's null!");}

        List<TreeObject<TCObject>> treeObjects = new ArrayList<TreeObject<TCObject>>();
        for(TCObject object:tcObjects){
            TreeObject<TCObject> treeObject = new TreeObject<TCObject>();
            treeObject.setId(String.valueOf(object.getObjectId()));
            treeObject.set_parentId(String.valueOf(object.getParentId()));
            treeObject.setText(object.getObjectName()+"("+object.getObjectCode()+")");
            if(object.getHasChildren() == 1){treeObject.setState("closed");}
            if(object.getType().equals(ObjectTypeEnum.FOLDER.getType()) || object.getType().equals(ObjectTypeEnum.FATHER.getType())){treeObject.setState("closed");}
            treeObject.setInfo(object);
            treeObjects.add(treeObject);
        }
        return treeObjects;
    }

    public static List<TreeObject<ObjectDTO>> changeToTreeObjectDTO(Collection<ObjectDTO> objectDTOs) throws ObjectServiceException{
        if(objectDTOs == null){throw new ObjectServiceException("Invalic tcObjects param! It's null!");}

        List<TreeObject<ObjectDTO>> treeObjects = new ArrayList<TreeObject<ObjectDTO>>();
        for(ObjectDTO object:objectDTOs){
            TreeObject<ObjectDTO> treeObject = new TreeObject<ObjectDTO>();
            treeObject.setId(String.valueOf(object.getObjectId()));
            treeObject.set_parentId(String.valueOf(object.getParentId()));
            treeObject.setText(object.getObjectName()+"("+object.getObjectCode()+")");
            if(object.getHasChildren() == 1){treeObject.setState("closed");}
            if(object.getType().equals(ObjectTypeEnum.FOLDER.getType()) || object.getType().equals(ObjectTypeEnum.FATHER.getType())){treeObject.setState("closed");}
            treeObject.setInfo(object);
            treeObjects.add(treeObject);
        }
        return treeObjects;
    }


    public static JsonConfig getTCObjectJsonConfig()throws ObjectServiceException{
        JsonConfig jc = new JsonConfig();
        jc.registerJsonBeanProcessor(Timestamp.class,new JsDateJsonBeanProcessor());
        return jc;
    }
}
